mod converter;

use clap::{Arg, ArgAction, Command};
use converter::convert_swagger_to_api;
use std::fs;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let matches = Command::new("gen_api_json")
        .version("1.0")
        .author("caozongying<zongying_cao@163.com>")
        .about("caozongying_cao<zongying_cao@163.com> is the author of gen api json")
        .arg(
            Arg::new("source")
                .short('s')
                .long("source")
                .value_name("SOURCE")
                .help("源文件/资源的路径")
                .action(ArgAction::Set),
        )
        .arg(
            Arg::new("target")
                .short('t')
                .long("target")
                .value_name("TARGET")
                .help("输出json文件的路径")
                .action(ArgAction::Set),
        )
        .get_matches();

    let source_path = matches
        .get_one::<String>("source")
        .expect("source的获取出错");
    let target_path = matches
        .get_one::<String>("target")
        .expect("目标文件的获取出错");

    let swagger_content = fs::read_to_string(source_path)?;

    let api_json = convert_swagger_to_api(swagger_content)?;

    fs::write(target_path, format!("{}", api_json))?;

    Ok(())
}
